package com.hg.hggood.controller;

import com.hg.hggood.entity.Order;
import com.hg.hggood.service.IOrderService;
import com.hg.hggood.util.JsonResult;
import com.hg.hggood.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@CrossOrigin(value = "http://localhost:8081")
@RequestMapping("orders")
@RestController//@Controller+@ResponseBodyX
public class OrderController extends BaseController{
    @Autowired
    private IOrderService orderService;

    @RequestMapping("create")
    public JsonResult<Order> create(Integer aid, Integer[] cids, HttpSession session){
        //create(Integer aid, Integer uid, String username, Integer[] cids);
        Integer uid = getuidFromSession(session);
        String username = getUsernameFromSession(session);
        Order data = orderService.create(aid, uid, username, cids);
        return new JsonResult<>(OK,data);
    }

    @RequestMapping({"","/"})
    public JsonResult<List<OrderVO>> getVOByUid(HttpSession session){
        List<OrderVO> data = orderService.getVOByUid(getuidFromSession(session));
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("{oid}/delete")
    public JsonResult<Void> delete(@PathVariable("oid") Integer oid,
                                   HttpSession session){
        orderService.delete(
                oid,
                getuidFromSession(session),
                getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }

    @RequestMapping("{id}/deleteOItemId")
    public JsonResult<Void> deleteOrderItemId(@PathVariable("id") Integer id,
                                   HttpSession session){
        orderService.deleteOrderItemId(
                id,
                getuidFromSession(session),
                getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }
}
